package cn.bluedot.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageInfo;

import cn.bluedot.dao.OutgoingRecordMapper;
import cn.bluedot.dao.ResMapper;
import cn.bluedot.dao.ResTypeMapper;
import cn.bluedot.dao.StorageRecordMapper;
import cn.bluedot.dao.UserMapper;
import cn.bluedot.model.OutgoingRecord;
import cn.bluedot.model.OutgoingRecordExample;
import cn.bluedot.model.PageBean;
import cn.bluedot.model.Res;
import cn.bluedot.model.ResExample;
import cn.bluedot.model.ResType;
import cn.bluedot.model.ResTypeExample;
import cn.bluedot.model.StorageRecord;
import cn.bluedot.model.StorageRecordExample;
import cn.bluedot.model.StorageRecordVo;
import cn.bluedot.model.User;
import cn.bluedot.service.ResService;
import cn.bluedot.util.Date2String;
@Service
@Transactional
public class ResServiceImpl implements ResService {
    
    @Autowired
    private ResTypeMapper resTypeDao;
    @Autowired
    private ResMapper resDao;
    @Autowired
    private StorageRecordMapper storageRecordDao;
    @Autowired
    private OutgoingRecordMapper outgoingRecordDao;
    @Autowired
    private UserMapper userDao;
    
    @Override
    public void addResType(ResType resType) {
        resTypeDao.insertSelective(resType);
    }

    @Override
    public List<ResType> getAllResType() {
        return resTypeDao.selectByExample(new ResTypeExample());
    }

    @Override
    public void addRes(Res res) {
        resDao.insertSelective(res);
    }
    
    @Override
    public void warehousing(StorageRecord storage) {
        storageRecordDao.insertSelective(storage);
    }

    @Override
    public void addOutGoing(OutgoingRecord outgoing) {
        outgoingRecordDao.insert(outgoing);
        System.out.println(outgoing);
    }

    @Override
    public List<StorageRecordVo> getListStorageRecordVO(int pageIndex) {
        PageBean pb = new PageBean((int)storageRecordDao.countByExample(new StorageRecordExample()), pageIndex);
        List<StorageRecord> selectList = storageRecordDao.selectList(pb.getLimit_start(), pb.getLimit_offset());
        List<StorageRecordVo> res = new ArrayList<>();
        for (StorageRecord sr : selectList) {
            StorageRecordVo srv = new StorageRecordVo();
            srv.setStandard(sr.getStandard());
            Res t_res = resDao.selectByPrimaryKey(sr.getResId());
            ResType rt = resTypeDao.selectByPrimaryKey(t_res.getResTypeId());
            srv.setResType(rt.getName());
            srv.setResName(t_res.getName());
            srv.setPrice(sr.getPrice());
            srv.setLastOutTime(Date2String.defaultD2S(sr.getMtime()));
            User u = userDao.selectByPrimaryKey(sr.getInPersonId());
            srv.setInPersonName(u.getName());
            srv.setId(sr.getId());
            srv.setCurN_InN(sr.getCurNumber() + "/" + sr.getInNumber());
            res.add(srv);
        }
        return res;
    }

    @Override
    public List<Res> getAllResByTypeID(long typeId) {
        
        ResExample example = new ResExample();
        example.createCriteria().andResTypeIdEqualTo(typeId);
        return resDao.selectByExample(example);
    }

    @Override
    public StorageRecord getStorageById(Long id) {
        return storageRecordDao.selectByPrimaryKey(id);
    }

    @Override
    public List<OutgoingRecord> getListOutgoingByStorageRecordId(Long storageRecordId) {
        
        OutgoingRecordExample example = new OutgoingRecordExample();
        example.createCriteria().andStorageRecordIdEqualTo(storageRecordId);
        return outgoingRecordDao.selectByExample(example);
    }

}
